package com.situ.employee2024.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.employee2024.dao.EmployeeDao;
import com.situ.employee2024.model.Employee;
import com.situ.employee2024.service.EmployeeService;
import com.situ.employee2024.util.PaginateInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 功能：
 *
 * @author 千堆雪
 * @version 1.0.0
 * @since 2024/1/10
 * <p>
 * created by 千堆雪 on 2024/1/10, last modified by 千堆雪 on 2024/1/10
 */
@Service
public class EmployeeServiceImpl implements EmployeeService {
    private EmployeeDao employeeDao;

    @Autowired
    public void setEmployeeDao(EmployeeDao employeeDao) {
        this.employeeDao = employeeDao;
    }

    @Override
    public List<Employee> findAll(PaginateInfo pi, Employee employee) {
        //第1步：在本地线程变量中存储分页信息，指定页码和页面大小
        try (Page<?> p = PageHelper.startPage(pi.getPageNo(), pi.getPageSize());) {
            //第2步：发起正常查询
            List<Employee> list = employeeDao.findAll(employee);

            //第3步：获取分页数据，分页对象
            PageInfo<Employee> pif = new PageInfo<>(list);
            pif.calcByNavigatePages(5);//共显示5页导航
            pi.setPageInfo(pif);
            return list;
        }
    }

    @Override
    public Employee findById(Integer id) {
        return employeeDao.findById(id);
    }

    @Override
    public int deleteByIds(Integer[] ids) {
        return employeeDao.deleteByIds(ids);
    }

    @Override
    public boolean save(Employee employee) {
        return employeeDao.save(employee) > 0;
    }

    @Override
    public boolean update(Employee employee) {
        return employeeDao.update(employee) > 0;
    }
}
